package com.zc.activity;



import java.io.File;
import java.util.ArrayList;
import java.util.FormatFlagsConversionMismatchException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zc.activity.RocketActivity.MyListViewAdapter;
import com.zc.activity.RocketActivity.MyListViewAdapter.ViewHolder;
import com.zc.getinfo.AppInfo;
import com.zc.getinfo.ClearFileInfo;
import com.zc.manager.AppManager;
import com.zc.manager.ClearCacheFileManager;
import com.zc.manager.MyDataCleanManager;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.media.Image;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.Formatter;
import android.view.TextureView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;


public class SdCleanActivity extends Activity {

	TextView allsize;
	ListView listView;
	CheckBox allclear_checkBox;
	Button allclear_btn;
	long allSize;
	SQLiteDatabase db;
	ProgressBar progressBar;
	MyListViewAdapter adapter;
	ImageView back;
	List<AppInfo> appList = new ArrayList<AppInfo>();
	Map<Integer, Boolean> checkedStatus = new HashMap<Integer, Boolean>();
	List<ClearFileInfo> clearFileInfo = new ArrayList<ClearFileInfo>();
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if (msg.arg1 == 1) {
				listView.setVisibility(View.VISIBLE);
				progressBar.setVisibility(View.GONE);
				allsize.setText(Formatter.formatFileSize(SdCleanActivity.this, allSize));
				adapter = new MyListViewAdapter();
				listView.setAdapter(adapter);
			}
		};
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sd_clean);
		initView();
		initData();
		

	}
	private void initData() {
		// TODO Auto-generated method stub
		listView.setVisibility(View.GONE);
		progressBar.setVisibility(View.VISIBLE);
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				ClearCacheFileManager cacheFileManager = new ClearCacheFileManager(SdCleanActivity.this);
				db = ClearCacheFileManager.getinstance(SdCleanActivity.this, getPackageName());
				AppManager appManager = new AppManager(SdCleanActivity.this);
				appList = appManager.getAllUserAppInfos();
				try {
					clearFileInfo = cacheFileManager.getApkFilePath(db, appList);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				for (ClearFileInfo clearFileInfo : clearFileInfo) {
					allSize += clearFileInfo.size;
				}
				
				Message message = new Message();
				message.arg1 = 1;
				handler.sendMessage(message);
			}
		}).start();						
		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		allclear_checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					for (int i = 0; i < clearFileInfo.size(); i++) {
						checkedStatus.put(i, isChecked);
					}
					adapter.notifyDataSetChanged();
				} else {
					for (int i = 0; i < clearFileInfo.size(); i++) {
						checkedStatus.remove(i);
					}
					adapter.notifyDataSetChanged();
				}

			}
		});
		allclear_btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
//		
//		for (ClearFileInfo clearFileInfo : clearFileInfo) {
//			System.out.println(clearFileInfo);
//		}
	}
	private void initView() {
		// TODO Auto-generated method stub
		listView = (ListView) findViewById(R.id.lajiListView);
		allclear_checkBox = (CheckBox) findViewById(R.id.alllaji_checkBox);
		allclear_btn = (Button) findViewById(R.id.clear);
		allsize = (TextView) findViewById(R.id.alllaji);
		back = (ImageView) findViewById(R.id.back);
		progressBar = (ProgressBar) findViewById(R.id.lajiProgressBar);
	}
	
	class MyListViewAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return clearFileInfo.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return clearFileInfo.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = getLayoutInflater().inflate(R.layout.list_item, null);
				holder.checkBox = (CheckBox) convertView.findViewById(R.id.soft_checkBox);
				holder.icon = (ImageView) convertView.findViewById(R.id.soft_icon);
				holder.lable = (TextView) convertView.findViewById(R.id.soft_label);
				holder.packageName = (TextView) convertView.findViewById(R.id.soft_packageName);
				holder.soft_version = (TextView) convertView.findViewById(R.id.soft_versionCode);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.packageName.setVisibility(View.INVISIBLE);
			holder.icon.setBackground(clearFileInfo.get(position).icon);
			holder.lable.setText(clearFileInfo.get(position).label);
			holder.soft_version.setText(Formatter.formatFileSize(SdCleanActivity.this, clearFileInfo.get(position).size));

			holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// TODO Auto-generated method stub
					checkedStatus.put(position, isChecked);
				}
			});

			if (checkedStatus != null && checkedStatus.containsKey(position)) {
				holder.checkBox.setChecked(checkedStatus.get(position));
			} else {
				holder.checkBox.setChecked(false);
			}

			return convertView;
		}

		class ViewHolder {
			CheckBox checkBox;
			ImageView icon;
			TextView lable;
			TextView soft_version;
			TextView packageName;
		}
	}

	
}
